home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8274 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: isonews.bbn.hp.com!hpbblb!news
  2. From: Matthias Dittrich <matti>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Permutations
  5. Date: 29 Feb 1996 18:30:15 GMT
  6. Organization: Hewlett-Packard Co.
  7. Message-ID: <4h4rbn$7es@hpbblb.bbn.hp.com>
  8. References: <Dn8yz9.GGy@spuddy.mew.co.uk> <4h1v27$cdc@texas.nwlink.com>
  9. NNTP-Posting-Host: trabant.bbn.hp.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (X11; I; HP-UX A.09.07 9000/712)
  14. X-URL: news:4h1v27$cdc@texas.nwlink.com
  15.  
  16. Teresa Reiko <tjr19@mail.nwlink.com> wrote:
  17. >david@spuddy.mew.co.uk (David Turner) wrote:
  18. >
  19. >> Does anyone know of an algorithm that works out the different permutations
  20. >>of ordering a number of objects? Not calculating how many there are, but 
  21. >>actaully working out (quickly?) what they permutations are!
  22. >
  23. >Untested code -- but this should work...
  24. >Example:  Permute("", "abcdef", 6)
  25. >
  26. >void Permute(char *p_prefix, char *p_list, int num)
  27. >{
  28. >        int i;
  29. >    char s[10], p[10];
  30. >
  31. >    if(num == 0)
  32. >    {
  33. >        printf("%s%s", prefix, p_list);
  34. Here is a little bug:          ^^^^^^
  35. you mean p_prefix I think.
  36.  
  37. (And the output is more readable if you append \n to the format string :-)
  38. >        return;
  39. >    }
  40. >
  41. >    for(i = 0; i < num; i++)
  42. >    {
  43. >        s[1] = 0;
  44. >        s[0] = p_list[i];
  45. >        strcpy(p, p_prefix);
  46. >        strcat(p, s);
  47. >        
  48. >        strncpy(s, p_list, i);
  49. >        s[i] = 0;
  50. >        strcat(s, p_list + i + 1);
  51. >
  52. >        Permute(p, s, num - 1);
  53. >    }
  54. >}       
  55. >...
  56.  
  57.